home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 4 / Mac Giga-ROM 4.0 - 1993.toast / FILES / BBS / HERMES / ProtMover6.1.cpt / sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-23  |  11.2 KB  |  425 lines  |  [TEXT/KAHL]

  1. #include "Protocol Mover.h"
  2. #include "Protocol.h"
  3.  
  4. /*#define        APPLUTIL        {dummy}*/
  5.  
  6. extern ProcMenuHandle PMH;
  7. extern Prefs thePrefs;
  8.  
  9. int cancelCount;
  10.  
  11. long tOne, tTwo, byteStart;
  12.  
  13. int procLevel[PROCINDEX];
  14.  
  15. DialogPtr Tdp = 0L;
  16. int (*ExtProc)() = 0L;
  17.  
  18. #define        tdCANCELBUT            1
  19. #define        tdSTATUS            2
  20. #define        tdERROR                3
  21. #define        tdMBNAME            4
  22. #define        tdMREFIN            5
  23. #define        tdMREFOUT            6
  24. #define        tdPROCID            7
  25. #define        tdFLAGS                8
  26. #define        tdFILESDONE            9
  27. #define        tdBYTESDONE            10
  28. #define        tdBYTESTOTAL        11
  29. #define        tdCPS                12
  30. #define        tdTIMEIN            13
  31. #define        tdTIMEOUT            14
  32. #define        tdDIRID                15
  33. #define        tdFILEID            16
  34. #define        tdVREFNUM            17
  35. #define        tdSLINE1            34
  36. #define        tdSLINE2            35
  37. #define        tdSLINE3            36
  38. #define        tdSLINE4            37
  39. #define        tdSLINE5            38
  40. #define        tdSLINE6            39
  41.  
  42. initProcLevel()
  43. {
  44.     register int i;
  45.     for (i = 0; i<PROCINDEX; i++) {
  46.         procLevel[i] = 0;
  47.     }
  48. }
  49.  
  50. doTdialog(eptr)
  51. EventRecord *eptr;
  52. {
  53.     int i, item;
  54.     DialogPtr dp;
  55.     if (DialogSelect(eptr, &dp, &item)) {
  56.         if (dp == Tdp) {
  57.             /*    check for cancel */
  58.             if (item == tdCANCELBUT) {
  59.                 if (cancelCount) {
  60.                     /*    drop "carrier" and dim button */
  61.                     (*(*PMH)->transData)->F.B.carrierLost = TRUE;
  62.                     ActItem(tdCANCELBUT, FALSE, Tdp);
  63.                 }
  64.                 else {
  65.                     /*    signal transfer to cancel and change button */
  66.                     (*(*PMH)->transData)->F.B.stopTrans = TRUE;
  67.                     SetCtlTitle(tdCANCELBUT, "\pEXIT", Tdp);
  68.                     ++cancelCount;
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75. showTdialog()
  76. {
  77.     OsErr err;
  78.     Rect aRect;
  79.     Handle pHand;
  80.     GrafPtr savePort;
  81.     int i, resSave, pID;
  82.     register ProtoRecPtr prp;
  83.     pascal void flashyStuff();
  84.     if (PMH && (*PMH)->transData && !Tdp) {
  85.         tOne = tTwo = 0;
  86.         prp = *(*PMH)->transData;
  87.         /*    save the current resource file */
  88.         resSave = CurResFile();
  89.         /*    set the resource file to the Transfer Protocols */
  90.         UseResFile(thePrefs.rRef);
  91.         /*    get the PROC resource */
  92.         pID = (*PMH)->pList[(*PMH)->transIndex].procID;
  93.         pHand = Get1Resource('PROC', pID);
  94.         /*    restore the resource file */
  95.         UseResFile(resSave);
  96.         if (pHand) {
  97.             /*    lock the PROC down if it is not being used...  for this
  98.                 example code this is not really needed, the need comes
  99.                 about in multiport systems where the same transfer PROC
  100.                 might be running on two different ports.  In this case
  101.                 it might already be locked down by "someone else".
  102.             */
  103.             if (!(procLevel[(pID-1000)/100]++)) {
  104.                 /*    move it up to help stop fragging */
  105.                 MoveHHi(pHand);
  106.                 /*    lock it down for the duration */
  107.                 HLock(pHand);
  108.             }
  109.             if ((*PMH)->transMessage == SETPREFS) {
  110.                 /*    let's take care of it here */
  111.                 /*    prp->mRefIn, mRefOut, timeOut to be filled out by
  112.                     caller... so let's do it!
  113.                 */
  114.                 prp->mRefIn = 0;
  115.                 prp->mRefOut = 0;
  116.                 prp->timeOut = 10;
  117.                 /*    get a pointer to it */
  118.                 ExtProc = (int (* )()) *pHand;
  119.                 /*    get it over with */
  120.                 /*    set the resource file to the Transfer Protocols */
  121.                 UseResFile(thePrefs.rRef);
  122.                 do {
  123.                     err = CallPascalW((*PMH)->transMessage, *(*PMH)->transData, 
  124.                                         (*PMH)->transRefCon, ExtProc);
  125.                 } while (!err);
  126.                 /*    restore the resource file */
  127.                 UseResFile(resSave);
  128.                 /*    show a MacOs Error (if there) */
  129.                 ErrorBox(err);
  130.                 /*    Unlock the PROC if it is not being used anymore...for
  131.                     this example code this is not really needed, the need
  132.                     comes about in multiport systems where the same
  133.                     transfer PROC might be running on two different ports.
  134.                     In this case it might be still running somewhere else,
  135.                     and therefore it must remain locked.
  136.                 */
  137.                 pID = (*PMH)->pList[(*PMH)->transIndex].procID;
  138.                 if (!(--procLevel[(pID-1000)/100])) {
  139.                     /*    PROC is marked purgable so we do not have to dispose
  140.                         of it
  141.                     */
  142.                     HUnlock(RecoverHandle(ExtProc));
  143.                 }
  144.                 /*    signal no more calls */
  145.                 ExtProc = 0L;
  146.                 /*    dispose of the file info data structure */
  147.                 Utility(DISPOSEPREC, PMH, 0L);
  148.             }
  149.             else {
  150.                 /*    prp->mRefIn, mRefOut, timeOut to be filled out by
  151.                     caller... so let's do it!
  152.                 */
  153.                 prp->mRefIn = thePrefs.mRefIn;
  154.                 prp->mRefOut = thePrefs.mRefOut;
  155.                 prp->timeOut = 10;
  156.                 /*    get a pointer to it */
  157.                 ExtProc = (int (* )()) *pHand;
  158.                 /*    reset the cancel button count */
  159.                 cancelCount = 0;
  160.                 /*    get the dialog and set the dialog's port up */
  161.                 GetPort(&savePort);
  162.                 SetDAFont(geneva);
  163.                 Tdp = GetNewDialog(129, 0L, -1L);
  164.                 SetDAFont(systemFont);
  165.                 SetPort(Tdp);
  166.                 TextSize(9);
  167.                 GetItemRect(tdSLINE1, &aRect, Tdp);
  168.                 SetDItem(Tdp, tdSLINE1, userItem, flashyStuff, &aRect);
  169.                 SetPort(savePort);
  170.                 /*    set up some items */
  171.                 SetNumberItem(tdMREFIN, (long) prp->mRefIn, Tdp);
  172.                 SetNumberItem(tdMREFOUT, (long) prp->mRefOut, Tdp);
  173.                 SetNumberItem(tdPROCID, (long) pID, Tdp);
  174.                 setFStats(0);
  175.                 byteStart = 0L;
  176.                 /*    disable all menus */
  177.                 for (i = 2; i<=TransferID; i++) {
  178.                     EDMenuItem(i, 0, FALSE);
  179.                 }
  180.                 DrawMenuBar();
  181.             }
  182.         }
  183.     }
  184. }
  185.  
  186. doTransPROC()
  187. {
  188.     OsErr err;
  189.     long cps;
  190.     int resSave, pID, i;
  191.     register ProcMenuPtr pmp;
  192.     register ProtoRecPtr prp;
  193.     if (ExtProc) {
  194.         prp = *(*PMH)->transData;
  195.         /*    save the current resource file */
  196.         tOne = TickCount();
  197.         resSave = CurResFile();
  198.         /*    set the resource file to the Transfer Protocols */
  199.         UseResFile(thePrefs.rRef);
  200.         /*    call the proc...dispose the dialog if we have an error */
  201.         pmp = *PMH;
  202.         err = CallPascalW(pmp->transMessage, *pmp->transData, 
  203.                             pmp->transRefCon, ExtProc);
  204.         /*    restore the resource file */
  205.         UseResFile(resSave);
  206.         /*    update the transfer dialog */
  207.         SetNumberItem(tdFLAGS, (long) prp->F.word, Tdp);
  208.         if (prp->F.B.Recovering) {
  209.             byteStart = prp->bytesDone;
  210.             prp->F.B.Recovering = FALSE;
  211.         }
  212.         if (prp->F.B.newError) {
  213.             HLock(prp->errReason);
  214.             SetTextItem(tdERROR, *prp->errReason, Tdp);
  215.             if (err) {
  216.                 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  217.                 /*    record error in transfer log */
  218.                 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  219.             }
  220.             HUnlock(prp->errReason);
  221.             DisposHandle(prp->errReason);
  222.             prp->errReason = 0L;
  223.             prp->F.B.newError = FALSE;
  224.         }
  225.         if (prp->F.B.newFile) {
  226.             /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  227.             /*    record transfer in log and update user stats */
  228.             /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  229.             prp->F.B.newFile = FALSE;
  230.             prp->bytesDone = 0L;
  231.             prp->bytesTotal = 0L;
  232.             setFStats(prp->filesDone+1);
  233.             byteStart = 0L;
  234.         }
  235.         if (prp->F.B.newMBName) {
  236.             setFStats(prp->filesDone);
  237.             prp->F.B.newMBName = FALSE;
  238.         }
  239.         SetNumberItem(tdFILESDONE, (long) prp->filesDone, Tdp);
  240.         SetNumberItem(tdBYTESDONE, (long) prp->bytesDone, Tdp);
  241.         SetNumberItem(tdBYTESTOTAL, (long) prp->bytesTotal, Tdp);
  242.         cps = (prp->bytesDone-byteStart)*60;
  243.         cps /= TickCount()-prp->startTime+1;
  244.         SetNumberItem(tdCPS, (long) cps, Tdp);
  245.         SetNumberItem(tdTIMEOUT, (long) (tOne - tTwo), Tdp);
  246.         tTwo = TickCount();
  247.         SetNumberItem(tdTIMEIN, (long) (tTwo - tOne), Tdp);
  248.         if (err) {
  249.             /*    close the transfer dialog */
  250.             SysBeep(3);
  251.             Delay(120L, &tTwo);
  252.             DisposDialog(Tdp);
  253.             /*    signal the dialog is gone */
  254.             Tdp = 0L;
  255.             /*    enable all menus */
  256.             for (i = 2; i<=TransferID; i++) {
  257.                 EDMenuItem(i, 0, TRUE);
  258.             }
  259.             DrawMenuBar();
  260.             /*    show a MacOs Error (if there) */
  261.             ErrorBox(err);
  262.             /*    Unlock the PROC if it is not being used anymore...for this
  263.                 example code this is not really needed, the need comes
  264.                 about in multiport systems where the same transfer PROC
  265.                 might be running on two different ports.  In this case it
  266.                 might be still running somewhere else, and therefore it
  267.                 must remain locked.
  268.             */
  269.             pID = (*PMH)->pList[(*PMH)->transIndex].procID;
  270.             if (!(--procLevel[(pID-1000)/100])) {
  271.                 /*    PROC is marked purgable so we do not have to dispose of it */
  272.                 HUnlock(RecoverHandle(ExtProc));
  273.             }
  274.             /*    signal no more calls */
  275.             ExtProc = 0L;
  276.             /*    dispose of the file info data structure */
  277.             Utility(DISPOSEPREC, PMH, 0L);
  278.         }
  279.     }
  280.     else if (thePrefs.mode == TERMINALMODE) {
  281.         checkAutoReceive();
  282.     }
  283. }
  284.  
  285. setFStats(index)
  286. int index;
  287. {
  288.     Str255 text1, text2;
  289.     register ProtoRecPtr prp;
  290.     text2[0] = 0;
  291.     prp = *(*PMH)->transData;
  292.     if (index < prp->fileCount) {
  293.         if ((*PMH)->transMessage == SEND) {
  294.             Pstrcpy(text1, "\pSending ā€œ");
  295.         }
  296.         else {
  297.             Pstrcpy(text1, "\pReceiving ā€œ");
  298.         }
  299.         Pstrcat(text1, *prp->fList[index].fName);
  300.         Pstrcat(text1, "\pā€ by ");
  301.         Pstrcat(text1, *(*PMH)->pList[(*PMH)->transIndex].subName);
  302.         SetNumberItem(tdVREFNUM, (long) prp->fList[index].vRefNum, Tdp);
  303.         SetNumberItem(tdDIRID, prp->fList[index].dirID, Tdp);
  304.         SetNumberItem(tdFILEID, prp->fList[index].fileID, Tdp);
  305.         if (prp->fList[index].mbName) {
  306.             Pstrcpy(text2, *prp->fList[index].mbName, Tdp);
  307.         }
  308.     }
  309.     else {
  310.         text1[0] = 0;
  311.         SetTextItem(tdVREFNUM, text1, Tdp);
  312.         SetTextItem(tdDIRID, text1, Tdp);
  313.         SetTextItem(tdFILEID, text1, Tdp);
  314.         Pstrcpy(text1, "\pFinishing Transfer");
  315.     }
  316.     SetTextItem(tdSTATUS, text1, Tdp);
  317.     SetTextItem(tdMBNAME, text2, Tdp);
  318. }
  319.  
  320. static pascal void flashyStuff(dp, item)
  321. DialogPtr dp;
  322. int item;
  323. {
  324.     Rect aRect;
  325.     PenPat(*((Handle) CurrentA5)-24);
  326.     GetItemRect(tdSLINE1, &aRect, dp);
  327.     FrameRect(&aRect);
  328.     GetItemRect(tdSLINE2, &aRect, dp);
  329.     FrameRect(&aRect);
  330.     GetItemRect(tdSLINE3, &aRect, dp);
  331.     FrameRect(&aRect);
  332.     GetItemRect(tdSLINE4, &aRect, dp);
  333.     FrameRect(&aRect);
  334.     GetItemRect(tdSLINE5, &aRect, dp);
  335.     FrameRect(&aRect);
  336.     GetItemRect(tdSLINE6, &aRect, dp);
  337.     FrameRect(&aRect);
  338.     PenNormal();
  339. }
  340.  
  341. checkAutoReceive()
  342. {
  343.     OsErr err;
  344.     char theChar;
  345.     register int i;
  346.     register AutoComPtr acpSave;
  347.     register unsigned char *acp;
  348.     long bytesAvail, count;
  349.     if (thePrefs.currentPort != -1) {
  350.         SerGetBuf(thePrefs.mRefIn, &bytesAvail);
  351.         if (bytesAvail) {
  352.             count = 1;
  353.             err = FSRead(thePrefs.mRefIn, &count, &theChar);
  354.             /*    of course a real terminal program would have to actually
  355.                 do something with the characters it gets but we don't...
  356.             */
  357.             if (!err) {
  358.                 acp = (unsigned char *) *(*PMH)->autoComs;
  359.                 for (i = 0; i<(*PMH)->autoCount; i++) {
  360.                     acpSave = (AutoComPtr) acp;
  361.                     /*    bump past the non-string stuff */
  362.                     acp = (unsigned char *) (&(((AutoComPtr) acp)->autoCom));
  363.                     if (*(acp + acpSave->matchChar) == theChar) {
  364.                         ++acpSave->matchChar;
  365.                         if (acpSave->matchChar > *acp) {
  366.                             /*    we have a match */
  367.                             Utility(DOMENU, PMH, acpSave->menuSelect);
  368.                             showTdialog();
  369.                             /*    clear all matchChars */
  370.                             acp = (unsigned char *) *(*PMH)->autoComs;
  371.                             for (i = 0; i<(*PMH)->autoCount; i++) {
  372.                                 ((AutoComPtr) acp)->matchChar = 1;
  373.                                 /*    bump past the non-string stuff */
  374.                                 acp = (unsigned char *)
  375.                                             (&(((AutoComPtr) acp)->autoCom));
  376.                                 /*    bump past the auto string (with padding) */
  377.                                 acp += *acp+2-(*acp & 1);
  378.                             }
  379.                             /*    break out of the loop */
  380.                             break;
  381.                         }
  382.                     }
  383.                     else {
  384.                         acpSave->matchChar = 1;
  385.                     }
  386.                     /*    bump past the auto string (with padding) */
  387.                     acp += *acp+2-(*acp & 1);
  388.                 }
  389.             }
  390.         }
  391.     }
  392. }
  393.  
  394. OsErr Utility(message, prp, refCon)
  395. register int message;
  396. register ProtoRecPtr prp;
  397. long refCon;
  398. {
  399.     int resSave;
  400.     Handle pHand;
  401.     if (thePrefs.rRef != -1) {
  402.         /*    save the current resource file */
  403.         resSave = CurResFile();
  404.         /*    set the resource file to the Transfer Protocols */
  405.         UseResFile(thePrefs.rRef);
  406. #ifndef APPLUTIL
  407.         /*    get the PROC resource */
  408.         pHand = Get1Resource('PROC', 900);
  409.         if (pHand) {
  410.             MoveHHi(pHand);
  411.             HLock(pHand);
  412.             ErrorBox(CallPascalW(message, prp, refCon, ((int (* )()) *pHand)));
  413.             HUnlock(pHand);
  414.         }
  415. #else
  416.         {
  417.             pascal OsErr UtilityMain();
  418.             ErrorBox(CallPascalW(message, prp, refCon, UtilityMain));
  419.         }
  420. #endif
  421.         /*    restore the resource file */
  422.         UseResFile(resSave);
  423.     }
  424. }
  425.